package com.cavy.platform.server.controller;

import com.cavy.platform.server.ResultInfo;
import com.cavy.platform.server.ResultList;
import com.cavy.platform.server.domain.Plugin;
import com.cavy.platform.server.dto.*;
import com.cavy.platform.server.service.PluginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * 本地插件操作
 */
@RestController
@RequestMapping("/plugin")
public class PluginController {

    @Autowired
    private PluginService service;

    //创建
    @PostMapping("/create")
    public ResultInfo create(@RequestBody @Valid PluginCreateDto dto) throws IOException {
        service.create(dto);
        return ResultInfo.newSuccess();
    }

    //上传logo
    @PostMapping("/uploadLogo")
    public ResultInfo uploadLogo(@Valid PluginLogoUploadDto uploadDto) {
        service.uploadLogo(uploadDto);
        return ResultInfo.newSuccess();
    }

    //修改
    @PostMapping("/modify")
    public ResultInfo modify(@RequestBody @Valid PluginModifyDto dto) throws IOException {
        service.modify(dto);
        return ResultInfo.newSuccess();
    }

    //删除
    @PostMapping("/delete")
    public ResultInfo delete(@RequestBody @Valid PluginDeleteDto dto) {
        service.delete(dto);
        return ResultInfo.newSuccess();
    }

    //列表
    @GetMapping("/list")
    public ResultList list(PluginSearchDto dto) {
        return service.list(dto);
    }

    //插件版本列表
    @GetMapping("/versionList")
    public String[] versionList(@RequestParam String name) {
        return service.versionList(name);
    }

    //详情
    @GetMapping("/info")
    public PluginDto info(@RequestParam String name, @RequestParam(defaultValue = Plugin.LATEST_VERSION) String version) throws IOException {
        return service.info(name, version);
    }

    //logo
    @GetMapping("/logo")
    public void logo(@RequestParam String name, HttpServletResponse response) {
        service.logo(name, response);
    }

    //刷新
    @PostMapping("/refresh")
    public ResultList refresh(PluginSearchDto searchDto) {
        return service.refresh(searchDto);
    }
}
